<?php
/*
Simple:Press Forum
Installer/Upgrader
$LastChangedDate: 2010-02-23 14:55:55 -0700 (Tue, 23 Feb 2010) $
$Rev: 3576 $
*/

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
	die('Access Denied');
}

require_once (SF_PLUGIN_DIR.'/sf-includes.php');
require_once (SF_PLUGIN_DIR.'/library/sf-primitives.php');

?>
<style type="text/css">

.imessage, .zmessage {
	display: none;
	width: 700px;
	height: auto;
	color: #000000;
	font-weight: bold;
	font-size: 11px;
	font-family: Tahoma, Helvetica, Arial, Verdana;
	margin: 2px 10px;
	padding: 5px;
	border: 2px solid #555555;
    -moz-border-radius: 5px;
    -khtml-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}

.imessage {
	background-color: #FFF799;
}

.zmessage {
	background-color: #A7C1FF;
}

.pbar {
	margin: 2px 20px;
	width: 685px;
}

#zonecount {
	display:none;
}

</style>
<?php

global $SFALLOPTIONS;
$SFALLOPTIONS = sf_load_alloptions();

# get current version  and build from database
$current_version = sf_get_option('sfversion');
$current_build = sf_get_option('sfbuild');

# check if we are coming back in with post values to install
if(isset($_POST['goinstall']))
{
	sf_go_install();
	return;
}

# check if we are coming back in with post values to upgrade
if(isset($_POST['goupgrade']))
{
	sf_go_upgrade($current_version, $current_build);
	return;
}

# Has the systen been installed?
if(version_compare($current_version, '1.0', '<'))
{
	sf_install_required();
	return;
}

# Base already installed - check Version and Build Number
if(($current_build < SFBUILD) || ($current_version > SFVERSION))
{
	sf_upgrade_required();
	return;
}

# set up install
function sf_install_required()
{
	?>
	<div class="wrap"><br />
		<?php $bad = sf_version_checks();
		if($bad != '') die($bad);
		?>
		<h2><?php _e("Install Simple:Press Forum Version", "sforum"); ?> <?php echo(SFVERSION); ?> - <?php _e("Build", "sforum"); ?> <?php echo(SFBUILD); ?></h2>
			<form name="sfinstall" method="post" action="admin.php?page=simple-forum/sf-loader-install.php"><br />
				<input type="submit" class="button-secondary" id="sbutton" name="goinstall" value="<?php esc_attr_e('Perform Installation', 'sforum'); ?>" />
			</form>
	</div>
	<?php
}

# set up upgrade
function sf_upgrade_required()
{
	?>
	<div class="wrap"><br />
		<?php $bad = sf_version_checks();
		if($bad != '') die($bad);
		?>
		<h2><?php echo sprintf(__("Upgrade Simple:Press Forum From Version %s to %s", "sforum"), sf_get_option('sfversion'), SFVERSION); ?>
		<br />
		(<?php _e("Build", "sforum"); ?> <?php echo(sf_get_option('sfbuild')); ?> <?php _e("to", "sforum"); ?> <?php _e("Build", "sforum"); ?> <?php echo(SFBUILD); ?>)</h2>
		<hr />
			<form name="sfupgrade" method="post" action="admin.php?page=simple-forum/sf-loader-install.php"><br />
				<input type="submit" class="button-secondary" id="sbutton" name="goupgrade" value="<?php esc_attr_e('Perform Upgrade', 'sforum'); ?>" />
			</form>
	</div>
	<?php
}

# perform install
function sf_go_install()
{
	global $wpdb, $current_user;

	add_option('sfInstallID', $current_user->ID); # use wp option table

    $phpfile = SFSITEURL."index.php?sf_ahah=install";
	$image = SFADMINIMAGES."working.gif";

	# how many users passes at 250 a pop?
	$users = $wpdb->get_var("SELECT COUNT(ID) FROM ".SFUSERS);
	$subphases = ceil($users / 250);
	$nextsubphase = 1;

	?>
	<div class="wrap"><br />
		<h2><?php _e("Simple:Press Forum is being installed", "sforum"); ?></h2>
		<div style="clear: both"></div>
		<br />
		<div class="wrap sfatag">
			<div class="imessage" id="imagezone"></div><br />
			<div class="pbar" id="progressbar"></div><br />
		</div>
		<div style="clear: both"></div>
		<table id="sfinstalltable" border="0" cellspacing="6" cellpadding="2">
			<tr><td><div class="zmessage" id="zone0"><?php __("Installing", "sforum"); ?>...</div></td></tr>
			<tr><td><div class="zmessage" id="zone1"></div></td></tr>
			<tr><td><div class="zmessage" id="zone2"></div></td></tr>
			<tr><td><div class="zmessage" id="zone3"></div></td></tr>
			<tr><td><div class="zmessage" id="zone4"></div></td></tr>
			<tr><td><div class="zmessage" id="zone5"></div></td></tr>
			<tr><td><div class="zmessage" id="zone6"></div></td></tr>
			<tr><td><div class="zmessage" id="zone7"></div></td></tr>
			<tr><td><div class="zmessage" id="zone8"></div></td></tr>
			<tr><td><div class="zmessage" id="zone9"></div></td></tr>
			<tr><td><div class="zmessage" id="zone10"></div></td></tr>
		</table>
		<div id="finishzone"></div>

<?php
		$pass = 10;
		$curr = 0;
		$messages = esc_js(__("Go to Forum Admin", "sforum"))."@".esc_js(__("Installation is in progress - please wait", "sforum"))."@".esc_js(__("Installation Completed", "sforum"))."@".esc_js(__("Installation has been Aborted", "sforum"));
		$out = '<script type="text/javascript">'."\n";
		$out.= 'sfjPerformInstall("'.$phpfile.'", "'.$pass.'", "'.$curr.'", "'.$subphases.'", "'.$nextsubphase.'", "'.$image.'", "'.$messages.'");'."\n";
		$out.= '</script>'."\n";
		echo $out;
?>
	</div>
	<?php
	return;
}

# perform upgrade
function sf_go_upgrade($current_version, $current_build)
{
	global $current_user;

	update_option('sfInstallID', $current_user->ID); # use wp option table
	sf_update_option('sfStartUpgrade', $current_build);

    $phpfile = SFSITEURL."index.php?sf_ahah=upgrade";
	$image = SFADMINIMAGES."working.gif";

	$targetbuild = SFBUILD;
	?>
	<div class="wrap"><br />
		<h2><?php _e("Simple:Press Forum is being upgraded", "sforum"); ?></h2>
		<br />
		<div class="wrap sfatag">
			<div class="imessage" id="imagezone"></div>
		</div><br />
		<div class="pbar" id="progressbar"></div><br />
		<div class="wrap sfatag">
			<div class="zmessage" id="errorzone"></div>
			<div id="finishzone"></div><br />
		</div><br />

<?php

		$messages = esc_js(__("Go to Forum Admin", "sforum"))."@".esc_js(__("Upgrade is in progress - please wait", "sforum"))."@".esc_js(__("Upgrade Completed", "sforum")).'@'.esc_js(__("Upgrade Aborted", "sforum"));
		$out = '<script type="text/javascript">'."\n";
		$out.= 'sfjPerformUpgrade("'.$phpfile.'", "'.$current_build.'", "'.$targetbuild.'", "'.$current_build.'", "'.$image.'", "'.$messages.'");'."\n";
		$out.= '</script>'."\n";
		echo $out;
?>
	</div>
	<?php
	return;
}

# Perform a MySQL server version check
function sf_version_checks()
{
	global $wp_version;

	if(function_exists("mysql_get_server_info"))
	{
		$message = '';

		# MySQL version check (4.1.21)
		$server = mysql_get_server_info();
		$mysql = true;
		$ver = explode(".", $server);
		if(!isset($ver[2])) $ver[2] = 0;
		if(intval($ver[0]) < 4) $mysql = false;
		if((intval($ver[0]) == 4) && (intval($ver[1]) < 1)) $mysql = false;
		if((intval($ver[0]) == 4) && (intval($ver[1]) == 1) && (intval($ver[2]) < 21)) $mysql = false;
		if(!$mysql)
		{
			$message = "<br /></div><div class='wrap sfatag'><p><b>".__("MySQL Version", "sforum")." ".$server.":</b><br /> ".__("Your version of MySQL is not supported by Simple:Press Forum", "sforum").SFVERSION.__("MySQL version 4.1.21 or above is required", "sforum")."</p>";
		}

		# WordPress (2.9)
		if(version_compare('2.9', $wp_version) == 1)
		{
			$message.= "<br /></div><div class='wrap sfatag'><p><b>".__("WordPress Version", "sforum")." ".$wp_version.":</b><br /> ".__("Your version of WordPress is not supported by Simple:Press Forum", "sforum").SFVERISON.__("WordPress version 2.9 or above is required", "sforum")."</p>";
		}
	}
	return $message;
}

?>